package cn.snmp.mutual.nms;

import java.net.*;

public class SnmpIpAddress extends SnmpString
{
    String ipAddr;
    private static final char[] digit;
    
    SnmpIpAddress() {
        this.ipAddr = null;
    }
    
    public SnmpIpAddress(final String s) {
        this.ipAddr = null;
        this.Type = 64;
        if (s != null && !s.equals("0.0.0.0")) {
            try {
                final InetAddress inetaddress = InetAddress.getByName(s);
                this.byteValue = inetaddress.getAddress();
            }
            catch (SecurityException securityexception) {
                System.err.println(SnmpUtils.getString("Security error creating SnmpIpAddress instance.") + " " + securityexception);
            }
            catch (UnknownHostException unknownhostexception) {
                System.err.println(SnmpUtils.getString("Error creating SnmpIpAddress : invalid String arg.") + " " + unknownhostexception);
            }
        }
        else {
            this.byteValue = new byte[4];
        }
    }
    
    public SnmpIpAddress(final byte[] abyte0) {
        this.ipAddr = null;
        this.Type = 64;
        this.byteValue = new byte[4];
        if (abyte0 != null && abyte0.length == 4) {
            System.arraycopy(abyte0, 0, this.byteValue, 0, abyte0.length);
        }
        else {
            System.err.println(SnmpUtils.getString("Error creating SnmpIpAddress : invalid byte arg."));
        }
    }
    
    @Override
    int encode(final byte[] abyte0, final int i) throws ArrayIndexOutOfBoundsException {
        return ASNTypes.encodeOctets(abyte0, i, this.byteValue, 0, this.byteValue.length, 64);
    }
    
    @Override
    public Object getVarObject() {
        return this.toString();
    }
    
    public static String netbToString(final byte[] abyte0) {
        final char[] ac = new char[abyte0.length * 4];
        int i = 0;
        final boolean flag = false;
        for (int k = 0; k < abyte0.length; ++k) {
            int j = abyte0[k] & 0xFF;
            if (j > 99) {
                ac[i++] = SnmpIpAddress.digit[j / 100];
                j %= 100;
                ac[i++] = SnmpIpAddress.digit[j / 10];
                j %= 10;
            }
            else if (j > 9) {
                ac[i++] = SnmpIpAddress.digit[j / 10];
                j %= 10;
            }
            ac[i++] = SnmpIpAddress.digit[j];
            ac[i++] = '.';
        }
        return new String(ac, 0, i - 1);
    }
    
    static SnmpVar newInstance(final byte[] abyte0) {
        final SnmpIpAddress snmpipaddress = new SnmpIpAddress();
        snmpipaddress.Type = 64;
        snmpipaddress.byteValue = abyte0;
        return snmpipaddress;
    }
    
    public String toHostName() {
        String s = this.toString();
        try {
            s = InetAddress.getByName(s).getHostName();
        }
        catch (UnknownHostException _ex) {}
        catch (SecurityException ex) {}
        return s;
    }
    
    @Override
    public String toString() {
        if (this.ipAddr == null && this.byteValue.length != 0) {
            this.ipAddr = netbToString(this.byteValue);
        }
        return this.ipAddr;
    }
    
    @Override
    public String toTagString() {
        return "IpAddress : " + this.toString();
    }
    
    @Override
    public Object toValue() {
        if (this.byteValue.length == 0) {
            return null;
        }
        return super.toString();
    }
    
    static {
        digit = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    }
}
